package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractSponsorCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillDctCmd;
import com.winchain.chainbill.bill.biz.discount.cmd.BillSerFeeSponsorCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillAttCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillInvoiceCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillExtCmd;
import com.winchain.chainbill.bill.biz.transfer.vo.BillSplitEvent;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctChdSubscriber extends OnErrorNotImplementedSubscriber<BillSplitEvent<CbDct>> {
	@Autowired
	private BillOptCmd<CbDct> billOptCmd;
	@Autowired
	private BillExtCmd<CbDct> billExtCmd;
	@Autowired
	private BillDctCmd billDctCmd;
	@Autowired
	private BillMoveCmd<CbDct> billMoveCmd;
	@Autowired
	private ContractSponsorCmd<CbDct> contractSponsorCmd;
	@Autowired
	private BillInvoiceCmd<CbDct> billInvoiceCmd;
	@Autowired
	private BillAttCmd<CbDct> billAttCmd;
	@Autowired
	private BillSerFeeSponsorCmd billSerFeeSponsorCmd;

	public void onApplicationEvent(BillSplitEvent<CbDct> t) {
		List<Bill<CbDct>> bills = (List<Bill<CbDct>>) t.getChildren();
		for (Bill<CbDct> bill : bills) {
			CommandContext<Bill<CbDct>> context = new CommandContext();
			context.setSource(bill);
			CbOpt opt = new CbOpt();
			opt.setCurState("S005");
			opt.setOptCode(null);
			opt.setRemark("��������");
			ChainBase<Bill<CbDct>> crb = new ChainBase();
			if (bill.getSource() != null) {
				opt.setLimDate(((CbDct) bill.getSource()).getDctDate());
				((CbDct) bill.getSource()).setDctQtOpt("0");
				context.setValue("bizId", ((CbDct) bill.getSource()).getDctId());
				context.setValue("attInvoices", t.getValue("attInvoices"));
				context.setValue("invType", t.getValue("invType"));
				crb.add(this.billAttCmd);
				crb.add(this.billInvoiceCmd);
			}
			context.setValue("Operation", opt);
			crb.add(this.billOptCmd);
			crb.add(this.billExtCmd);

			crb.add(this.billDctCmd);
			crb.add(this.billMoveCmd);
			crb.add(this.contractSponsorCmd);
			crb.add(this.billSerFeeSponsorCmd);

			crb.execute(context);
		}
	}
}
